//-----------------------------------------------------------------------------
// Filename: UACInviteTransaction.cs
//
// Description: SIP Transaction that implements UAC (Sser Agent Client) functionality for
// an INVITE transaction.
// 
// History:
// 21 Nov 2006	Aaron Clauson	Created.
//
// License: 
// This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
//
// Copyright (c) 2006 Aaron Clauson (aaronc@blueface.ie), Blue Face Ltd, Dublin, Ireland (www.blueface.ie)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that 
// the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
// disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Blue Face Ltd. 
// nor the names of its contributors may be used to endorse or promote products derived from this software without specific 
// prior written permission. 
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
//-----------------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using BlueFace.VoIP.Authentication;
using CN.Sys;
using CN.Sys.Net;
using log4net;

#if UNITTEST
using NUnit.Framework;
#endif

namespace CN.VoIP.Net.SIP
{
	/// <summary>
	/// SIP transaction that initiates a call to a SIP Server User Agent.
	/// </summary>
    public class UACInviteTransaction : SIPTransaction
    {       
        //private IPEndPoint m_serverEndPoint;

        protected SIPRequest m_ackRequest;               // ACK request for INVITE transactions.
        protected IPEndPoint m_ackRequestIPEndPoint;     // Socket the ACK request was sent to.

        public UACInviteTransaction(
            SIPRequest sipRequest)
            : base(sipRequest)
		{
            TransactionType = SIPTransactionTypesEnum.Invite;
        }

        public override void GotRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, SIPRequest sipRequest)
        {
            logger.Debug("UACInviteTransaction received unexexpected request, " + sipRequest.Method + " from " + IPSocket.GetSocketString(remoteEndPoint) + ", ignoring.");
        }

        public override void GotResponse(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, SIPResponse sipResponse)
        {
            try
            {
                logger.Debug("UACInviteTransaction Server response " + sipResponse.StatusCode + " " + sipResponse.ReasonPhrase + ".");

                if (TransactionState != SIPTransactionStatesEnum.Completed)
                {
                    if (sipResponse.StatusCode >= 100 && sipResponse.StatusCode < 199)
                    {
                        TransactionState = SIPTransactionStatesEnum.Proceeding;
                        base.FireTransactionStateChangedEvent();

                        FireTransactionInformationResponseReceivedEvent(localEndPoint, remoteEndPoint, sipResponse);
                    }
                    else 
                    {
                        m_finalResponse = sipResponse;
  
                        // BranchId for 2xx responses needs to be a new one, non-2xx final responses use same one as original request.
                        SIPRequest ackRequest = null;
                        if (sipResponse.StatusCode >= 200 && sipResponse.StatusCode < 299)
                        {
                            SIPURI ackURI = m_initialRequest.URI;
                            if (sipResponse.Header.Contact != null && sipResponse.Header.Contact.Count > 0)
                            {
                                ackURI = sipResponse.Header.Contact[0].ContactURI;
                            }

                            ackRequest = GetAckRequest(sipResponse, ackURI, SIPTransport.GetTransportContact(null), CallProperties.CreateBranchId());
                        }
                        else
                        {
                            ackRequest = GetAckRequest(sipResponse, m_initialRequest.URI, SIPTransport.GetTransportContact(null), sipResponse.Header.Via.TopViaHeader.Branch);
                        }

                        TransactionState = SIPTransactionStatesEnum.Completed;
                        SendAckRequest(ackRequest);
                        
                        base.FireTransactionStateChangedEvent();
                        FireTransactionFinalResponseReceivedEvent(localEndPoint, remoteEndPoint, sipResponse);
                    } 
                }
                else
                {
                    logger.Debug("UACInviteTransaction Duplicate server response " + sipResponse.StatusCode + " " + sipResponse.ReasonPhrase + ", resending ACK.");
                    ResendAckRequest();
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception UACInviteTransaction GotResponse. " + excp.Message);
            }
        }

        public void SendInviteRequest(IPEndPoint dstEndPoint, SIPRequest inviteRequest)
        {
            logger.Debug("UACInviteTransaction sending INVITE for " + inviteRequest.URI.ToString() + " to " + IPSocket.GetSocketString(dstEndPoint) + ".");

            //m_serverEndPoint = dstEndPoint;
            this.RemoteEndPoint = dstEndPoint;

            TransactionState = SIPTransactionStatesEnum.Calling; 

            SIPTransport.SendSIPReliable(this);
            base.FireTransactionStateChangedEvent();
        }

        /// <summary>
        /// From RFC 3261 Chapter 17.1.1.3 - ACK for non-2xx final responses
        /// 
        /// The ACK request constructed by the client transaction MUST contain
        /// values for the Call-ID, From, and Request-URI that are equal to the
        /// values of those header fields in the request passed to the transport
        /// by the client transaction (call this the "original request").  The To
        /// header field in the ACK MUST equal the To header field in the
        /// response being acknowledged, and therefore will usually differ from
        /// the To header field in the original request by the addition of the
        /// tag parameter.  The ACK MUST contain a single Via header field, and
        /// this MUST be equal to the top Via header field of the original
        /// request.  The CSeq header field in the ACK MUST contain the same
        /// value for the sequence number as was present in the original request,
        /// but the method parameter MUST be equal to "ACK".
        ///
        /// If the INVITE request whose response is being acknowledged had Route
        /// header fields, those header fields MUST appear in the ACK.  This is
        /// to ensure that the ACK can be routed properly through any downstream
        /// stateless proxies.
        /// 
        /// From RFC 3261 Chapter 13.2.2.4 - ACK for 2xx final responses
        /// 
        /// The UAC core MUST generate an ACK request for each 2xx received from
        /// the transaction layer.  The header fields of the ACK are constructed
        /// in the same way as for any request sent within a dialog (see Section
        /// 12) with the exception of the CSeq and the header fields related to
        /// authentication.  The sequence number of the CSeq header field MUST be
        /// the same as the INVITE being acknowledged, but the CSeq method MUST
        /// be ACK.  The ACK MUST contain the same credentials as the INVITE.  If
        /// the 2xx contains an offer (based on the rules above), the ACK MUST
        /// carry an answer in its body.  If the offer in the 2xx response is not
        /// acceptable, the UAC core MUST generate a valid answer in the ACK and
        /// then send a BYE immediately.
        /// </summary>
        public SIPRequest GetAckRequest(SIPResponse sipResponse, SIPURI ackURI, IPEndPoint localEndPoint, string branchId)
        {
            //SIPURI dstURI = (sipResponse.Header.Contact != null && sipResponse.Header.Contact.Count > 0) ? sipResponse.Header.Contact[0].ContactURI : origURI;

            SIPRequest ackRequest = new SIPRequest(SIPMethodsEnum.ACK, ackURI.ToString());

            SIPHeader header = new SIPHeader(sipResponse.Header.From, sipResponse.Header.To, sipResponse.Header.CSeq, sipResponse.Header.CallId);
            header.CSeqMethod = SIPMethodsEnum.ACK.ToString();

            if (sipResponse.Header.RecordRoutes != null)
            {
                header.Routes = sipResponse.Header.RecordRoutes.Reversed();
            }

            ackRequest.Header = header;

            SIPViaHeader viaHeader = new SIPViaHeader(localEndPoint.Address.ToString(), localEndPoint.Port, branchId);
            ackRequest.Header.Via.PushViaHeader(viaHeader);

            return ackRequest;
        }

        public void ResendAckRequest()
        {
            //logger.Debug("Transaction resending ACK request for " + m_initialRequest.URI.ToString() + " and " + TransactionId + " to " + IPSocketAddress.GetSocketString(m_ackRequestIPEndPoint) + ".");

            SIPTransport.SendRequest(m_ackRequestIPEndPoint, m_ackRequest);
        }

        public void SendAckRequest(SIPRequest ackRequest)
        {
            m_ackRequest = ackRequest;
            //m_ackRequestIPEndPoint = ackRequest.GetRequestEndPoint();

            if (RemoteEndPoint == null)
            {
                if (SourceEndPoint != null)
                {
                    IPEndPoint dstEndPoint = InitialRequest.GetRequestEndPoint();
                    logger.Debug("TransactionLayer sending ACK request for " + m_initialRequest.URI.ToString() + " to " + dstEndPoint + ".");
                    SIPTransport.SendRequestFrom(SourceEndPoint, dstEndPoint, ackRequest);
                }
                else
                {
                    logger.Debug("TransactionLayer sending ACK request for " + m_initialRequest.URI.ToString() + ".");
                    SIPTransport.SendRequest(ackRequest);
                }
            }
            else
            {
                if (SourceEndPoint != null)
                {
                    logger.Debug("TransactionLayer sending ACK request for " + m_initialRequest.URI.ToString() + " to " + RemoteEndPoint + ".");
                    SIPTransport.SendRequestFrom(SourceEndPoint, RemoteEndPoint, ackRequest);
                }
                else
                {
                    logger.Debug("TransactionLayer sending ACK request for " + m_initialRequest.URI.ToString() + ".");
                    SIPTransport.SendRequest(RemoteEndPoint, ackRequest);
                }
            }
        }
	}
}
